package com.wang.project.file.serviceImpl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.wang.project.common.BaseResponse;
import com.wang.project.common.ErrorCode;
import com.wang.project.common.ResultUtils;
import com.wang.project.exception.BusinessException;
import com.wang.project.file.FilePreviewParam;
import com.wang.project.file.factory.FileEventService;
import com.wang.project.model.entity.User;
import com.wang.project.model.file.FileQueryUrlParam;
import com.wang.project.model.file.FileUploadParam;
import com.wang.project.model.file.FileUserAvatarUrlVO;
import com.wang.project.utils.FileUtils;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.errors.*;
import io.minio.http.Method;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * 文件预览入口
 */
@Component
@Slf4j
public class FilePreviewServiceImpl implements FileEventService {
    /**
     * 这里写文件预览的内容， 根据入参的类型区分是什么上传 比如用户头像上传 其他文件的上传，，不同的上传可能需要不同的处理，比如压缩，文件名 地址等等
     */

    @Autowired
    private MinioClient minioClient;


    public BaseResponse<Object> test(String param) {
        System.out.println(param);
        FilePreviewParam filePreviewParam = JSON.parseObject(param, FilePreviewParam.class);
        log.info("FilePreviewServiceImpl test : {}", JSONUtil.toJsonStr(filePreviewParam));
        log.info("FilePreviewServiceImpl filePreviewParam : {}", filePreviewParam.getType());
        log.info("FilePreviewServiceImpl filePreviewParam : {}", filePreviewParam.getRegion());
        FileUserAvatarUrlVO fileUserAvatarUrlVO = new FileUserAvatarUrlVO();
        fileUserAvatarUrlVO.setAvatarName(filePreviewParam.getType());
        fileUserAvatarUrlVO.setAvatarUrl(filePreviewParam.getRegion());
        log.info("FilePreviewServiceImpl fileUserAvatarUrlVO : {}", JSONUtil.toJsonStr(fileUserAvatarUrlVO));
        return ResultUtils.success(fileUserAvatarUrlVO);
    }

    /**
     * 头像地址  传递的参数 应该有  user_avatar || interface_avatar_
     *
     * @param param
     * @return
     */
    public Object avatarUrl(String param) {
//        FilePreviewParam filePreviewParam = JSON.parseObject(param, FilePreviewParam.class);
//        String userAvatarUrl = user.getUserAvatar();
//        //获取文件在线地址中的文件名
//        String originalFilename = FileUtils.getFileNameByUrl(userAvatarUrl);
//        if (originalFilename == null) {
//            log.info("FileServiceImpl getUserAvatarUrl originalFilename is null");
//            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "用户头像地址错误，请检查");
//        }
//        FileUploadParam fileUploadParam = new FileUploadParam();
//        fileUploadParam.setType(param.getType());
//        fileUploadParam.setRegion(param.getRegion());
//        log.info("FileServiceImpl getObjectURL originalFilename:{},fileUploadPara:{}", originalFilename, fileUploadParam);
//        String fileName = FileUtils.getMinioUploadFileName(fileUploadParam, user) + originalFilename.substring(originalFilename.lastIndexOf("."));
//        try {
//            return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucketName).object(fileName).build());
//        } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException |
//                 InvalidResponseException | IOException | NoSuchAlgorithmException | XmlParserException |
//                 ServerException e) {
//            throw new RuntimeException(e);
//        }
        return "";
    }
}
